package top.csy8888.ch06.config;

import org.springframework.boot.web.servlet.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import top.csy8888.ch06.filter.MyFilter;
import top.csy8888.ch06.filter.TestFilter;

import java.util.logging.Filter;

/**
 * @author CSY
 * @date 2025 - 09 - 12
 */
@Configuration
/**
 * @ServletComponentScan注解
 * 是springboot提供的专门用于扫描servlet组件（Servlet、Filter、Listener）
 * 注意：这种扫描有一个缺点：当有多个过滤器形成过滤链的时候，无法按照指定的顺序来执行
 */
//@ServletComponentScan(basePackages = "top.csy8888.ch06.filter")
public class MvcConfig {
    /**
     * 第二种方式：使用springboot提供的动态注册器（RegistrationBean）
     * 来注册servlet组件，优点是可以控制过滤链的执行顺序
     * 有三种动态注册器分别注册Servlet、Filter、Listener
     * ServletRegistrationBean
     * FilterRegistrationBean
     * ServletListenerRegistrationBean
     */
    @Bean
    public FilterRegistrationBean TestFilter(){
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        //装配过滤器
        filterRegistrationBean.setFilter(new TestFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.setName("testFilter");
        //设置优先级别 - 数值越小，优先级越高
        filterRegistrationBean.setOrder(1);
        return filterRegistrationBean;
    }
    @Bean
    public FilterRegistrationBean MyFilter(){
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        //装配过滤器
        filterRegistrationBean.setFilter(new MyFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.setName("myFilter");
        //设置优先级别 - 数值越小，优先级越高
        filterRegistrationBean.setOrder(2);
        return filterRegistrationBean;
    }
}
